# coding=utf-8 
"""
@Time    : 2018/05/28  下午 1:38
@Author  : hzsyy
@FileName: readYaml.py
@IDE     : PyCharm
"""

import yaml
import os
from getRootPath import root_dir


class operYaml:
	def __init__(self, yamlPath):
		self.yamlPath = yamlPath

	def caseList(self):
		with open(self.yamlPath, 'r', encoding='utf-8') as fp:
			contents = fp.read()
			testCase_dict = yaml.safe_load(contents)
			case_list = []
			for caseName, caseInfo in testCase_dict.items():
				new_dict = {}
				new_dict[caseName] = caseInfo
				case_list.append(new_dict)
			return case_list
	def wreatelist(self,c):
		m={}
		for v in c:
			for value,opens in v.items():
				m[value]=opens
		return m
	def write_yaml(self,response,post):
		with open(post, encoding='utf-8',mode='w') as f:
			try:
				yaml.dump(data=response,stream=f,allow_unicode=True)
			except Exception as e:
				print(e)
	def save_yaml(self,value,form,form2='ID'):
		with open(self.yamlPath,'r',encoding='utf-8') as c:
			m=c.read()
			r=yaml.safe_load(m)
			print(r)
		r[form]['data'][form2]=value
		
		with open(self.yamlPath,'w',encoding='utf-8') as f:
			yaml.dump(data=r,stream=f,allow_unicode=True)



if __name__ == "__main__":
	yaml_path = os.path.join(root_dir, "yamlCase", "福利社", "ShopDetail.yaml")
	oper_yaml = operYaml(yaml_path)
	case_list = oper_yaml.caseList()
	case_list[2]['取福利社详情id空']['data']['ID']=1
	e=oper_yaml.wreatelist(case_list)
	oper_yaml.write_yaml(e,yaml_path)
	# num = 0
	# for i in case_list[2:]:
	# 	num +=1
	# 	print(i)
	# print("共%d条用例" % num)







